package com.bjpowernode.springboot.web;

import com.bjpowernode.springboot.model.User;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @RequestMapping(value = "/each/list")
    public String eachList(Model model){
        List<User> userList = new ArrayList<>();
        for(int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setNick("张" + i);
            user.setPhone("00000" + i);
            user.setAddress("第" + i +"号大街");
            userList.add(user);
        }
        model.addAttribute("userList",userList);
        model.addAttribute("data","springBoot");
        return "each";
    }

    @RequestMapping(value = "/each/map")
    public String eachMap(Model model){
        Map<Integer,Object> userMaps = new HashMap<>();

        for(int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setNick("张" + i);
            user.setPhone("00000" + i);
            user.setAddress("第" + i +"号大街");
            userMaps.put(i,user);
        }

        model.addAttribute("userMaps",userMaps);
        return "eachMap";
    }

    @RequestMapping(value = "/each/array")
    public String eachArray(Model model){
        User[] userArray = new User[10];
        for(int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(100+i);
            user.setNick("张" + i);
            user.setPhone("00000" + i);
            user.setAddress("第" + i +"号大街");
            userArray[i]=user;
        }
        model.addAttribute("userArray",userArray);
        return "eachArray";
    }

    @RequestMapping(value = "/each/complex")
    public String complex(Model model){
        List<Map<Integer,List<User>>> myList = new ArrayList<>();
        for(int i = 0; i < 2; i++){
            Map<Integer,List<User>> myMap = new HashMap<>();
            for (int j = 0; j < 2; j++) {
                List<User> myUserList = new ArrayList<>();
                for (int k = 0; k < 3; k++) {
                    User user = new User();
                    user.setId(100+i);
                    user.setNick("张" + i);
                    user.setPhone("00000" + i);
                    user.setAddress("第" + i +"号大街");
                    myUserList.add(user);
                }
                myMap.put(j,myUserList);
            }
            myList.add(myMap);
        }
        model.addAttribute("mylist",myList);
        return "complex";
    }

}
